package servlet;

import entity.User;
import service.UserService;
import service.UserServiceImpl;
import util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project javase2203
 * @Package servlet
 * @Description:
 * @Date 2022/5/27 14:11
 */
@WebServlet( "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中午乱码问题
        request.setCharacterEncoding("utf-8");
        //获取前端传入的参数
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");


        //  ||有一个是空就直接返回
        if (MyUtil.isEmpty(uname) || MyUtil.isEmpty(pwd)) {
            response.sendRedirect(request.getContextPath() + "/login.html");
            return ;
        }
        //走登录的逻辑  调用业务层的内容
        UserService service = new UserServiceImpl();
        User user = service.login(uname, pwd);

        if (user != null) {
            request.getSession().setAttribute("uname", uname);
            //登录成功
            response.sendRedirect(request.getContextPath() + "/PersonalFileList");

        } else {
            //登录失败
            response.sendRedirect(request.getContextPath() + "/login.html");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}








